<?php 
namespace app\common\service\queue;
use think\facade\Db;
use app\common\model\Resume as ResumeModel;
use app\common\model\ResumeRefreshAuto as ResumeRefreshAutoModel;
use app\common\model\ResumeRefreshAutoLog as ResumeRefreshAutoLogModel;
class Resume
{
    public function resume_auto_refresh_handle()
    {
        $resumeAuto=ResumeRefreshAutoModel::where(['status'=>1])
            ->whereTime('next_time', '<=', time())->find();
        if ($resumeAuto) {
            $resumeAuto->sum=Db::raw('sum+1');
            $resumeAuto->next_time=time()+3600*4;
            $ResumeModel=ResumeModel::where(['id'=>$resumeAuto->resume_id])->find();
            $ResumeModel->refresh_time=time();
            $ResumeRefreshAutoLogModel=new ResumeRefreshAutoLogModel;
            $ResumeRefreshAutoLogModel->resume_id=$resumeAuto->resume_id;
            Db::startTrans();
            try {
                $resumeAuto->save();
                $ResumeModel->save();
                $ResumeRefreshAutoLogModel->save();
                // 提交事务
                Db::commit();
            } catch (\Exception $e) {
                //回滚事务
                Db::rollback();
            }
        }
    }
    public function resume_auto_refresh_expire_handle()
    {
        $ResumeRefreshAutoModel=ResumeRefreshAutoModel::where(['status'=>1])
            ->whereTime('end_time', '<=', time())->field('id,status')->select()->toArray();
        foreach ($ResumeRefreshAutoModel as $k => $v) {
            $ResumeRefreshAutoModel[$k]['status']=0;
        }
        (new ResumeRefreshAutoModel)->saveAll($ResumeRefreshAutoModel);
    }
    public function resume_top_expire_handle()
    {
        $ResumeModel=ResumeModel::where(['top'=>1])
            ->whereTime('top_time', '<=', time())->field('id,top')->select()->toArray();
        if(!empty($ResumeModel)){
            foreach ($ResumeModel as $k => $v) {
                $ResumeModel[$k]['top']=0;
            }
            (new ResumeModel)->saveAll($ResumeModel);
        }
    }
    public function resume_recommend_expire_handle()
    {
        $ResumeModel=ResumeModel::where(['recommend'=>1])
            ->whereTime('recommend_time', '<=', time())->field('id,recommend')->select()->toArray();
        if(!empty($ResumeModel)){
            foreach ($ResumeModel as $k => $v) {
                $ResumeModel[$k]['recommend']=0;
            }
            (new ResumeModel)->saveAll($ResumeModel);
        }
    }
}